package cn.ly.link.order;

public class OrderLinkList<E> {
	private Node1 first;
	public OrderLinkList(){
		this.first = null;
	}
	
	public OrderLinkList(Node1[] linkArr){
		first = null;
		for(int i=0; i<linkArr.length; i++){
			insert(linkArr[i]);
		}
	}
	
	public void insert(Node1 node){
		Node1 previous = null;
		Node1 current = this.first;
		while(current != null && node.data > current.data){
			previous = current;
			current = current.next;
		}
		if(previous == null){
			this.first = node;
		}else{
			previous.next = node;
		}
		node.next = current;
	}
	
	/**
	 * 新节点需要插入在 前一个节点的后面，和当前节点的前面
	 * @param data
	 */
	public void insert(int data){
		Node1 newNode = new Node1(data);
		Node1 previous = null;
		Node1 current = this.first;
		
		while(current != null && data > current.data){
			previous = current;
			current = current.next;
		}
		if(previous == null){
			this.first = newNode;
		}else{
			previous.next = newNode;
		}
		newNode.next = current;
	}
	
	public int remove(){
		Node1 temp = first;
		first = first.next;
		return temp.data;
	}
	
	public void display(){
		Node1 cur = first;
		while(cur != null){
			cur.display();
			cur = cur.next;
		}
		System.out.println();
	}

}
